package cn.gson.crm.controller.potential;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.EmploDao;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.entity.Student;

/**
 * 学生一键分配
 */
@WebServlet("/oneKeyAllocation")
public class StudentOneKeyAllocation extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private StudentDao stuDao = new StudentDao();
	private EmploDao empDao = new EmploDao();
       
	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.get(request, response);
		List<Student> stuList = stuDao.showAllNotAllocationStu("正常", "未分配");
		List<Employee> empList = empDao.allEmploGetDeptId("正常", 11);
		//获取咨询师id
		Integer[] eid = new Integer[empList.size()];
		//获取潜在学生id
		Integer[] sid = new Integer[stuList.size()];
		for (int i = 0; i < empList.size(); i++) {
			eid[i] = empList.get(i).getId();
		}
		System.out.println("咨询师:"+eid.length);
		for (int i = 0; i < stuList.size(); i++) {
			sid[i] = stuList.get(i).getId();
		}
		System.out.println("学生师:"+sid.length);
		//10,3  3  1
		int rid = sid.length % eid.length;
		int avr = sid.length / eid.length;
		if(avr == 0){
			for (int i = 0; i < sid.length; i++) {
				stuDao.allocationStu("已分配", eid[i], sid[i]);
			}
		}else{
			int h=eid.length * avr;
			for (int i = 0; i < eid.length; i++) {
				for (int j = 0; j < avr; j++) {
					stuDao.allocationStu("已分配", eid[i], sid[h-1]);
					h--;
				}
			}
			System.out.println(h);
			
			for (int i = eid.length * avr; i < sid.length; i++) {
				stuDao.allocationStu("已分配", eid[h], sid[i]);
				h++;
			}
			
			
		}
		request.getRequestDispatcher("potential").forward(request, response);
	}
}
